package com.kzd.btgame.main.mine

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.btgame.R
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.databinding.IncludeAppRefreshBinding
import com.kzd.btgame.entity.GameBean
import com.kzd.btgame.jetpack.vm.MineViewModel
import com.kzd.btgame.main.game.adapter.Game1Adapter
import com.kzd.btgame.main.mine.adapter.MineDiscussAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class MineFootprintFragment : OwnerViewBindingFragment<IncludeAppRefreshBinding>() {

    companion object {
        const val KEY_COLLECT_ACTION = "KEY_COLLECT_ACTION"
        fun newInstance(action: Int): MineFootprintFragment {
            val bundle = Bundle().apply {
                putInt(KEY_COLLECT_ACTION, action)
            }
            return MineFootprintFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mGame1Adapter: Game1Adapter<GameBean> by lazy {
        Game1Adapter()
    }

    private val mMineDiscussAdapter: MineDiscussAdapter by lazy {
        MineDiscussAdapter()
    }

    private val mAction: Int by lazy {
        arguments?.getInt(KEY_COLLECT_ACTION) ?: 0
    }

    private val mViewModel: MineViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> IncludeAppRefreshBinding
        get() = IncludeAppRefreshBinding::inflate

    override fun initView() {
        mViewBinding.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = if (mAction == 0)
            mGame1Adapter else mMineDiscussAdapter

        mViewBinding.mSmartRefreshLayout.setBackgroundResource(
            if (mAction == 0) R.color.white else R.color.background
        )

        mViewBinding.mSmartRefreshLayout.setOnRefreshListener {
            initData()
        }
        mViewBinding.mSmartRefreshLayout.setOnLoadMoreListener {
            mViewModel.mineMyDiscuss(isLoadMore = true)
        }
    }

    override fun initData() {
        if (mAction == 0) {
            mViewModel.mineMyGame()
        } else {
            mViewModel.mineMyDiscuss()
        }
    }

    override fun initViewMode() {

        mViewModel.myGameLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response, mGame1Adapter, mViewBinding.mSmartRefreshLayout, (false))
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mGame1Adapter.setNewInstance(data)
        })

        mViewModel.mineDiscussLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response, mMineDiscussAdapter, mViewBinding.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mMineDiscussAdapter.addData(data)
            } else {
                mMineDiscussAdapter.setNewInstance(data)
            }
        })
    }

}